package com.yamaha.bikedesalon;

import java.net.SocketTimeoutException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.facebook.Session;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.testflightapp.lib.TestFlight;
import com.yamaha.bikedesalon.util.API;
import com.yamaha.bikedesalon.util.SCStaticFunc;

public class SplashFragment extends Activity {

	private final String TAG = getClass().getSimpleName();

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);

		if (!Utils.isNetworkConnected(this)) {
			return;
		}

		final Session session = Session.getActiveSession();
		if (session != null) {
			session.closeAndClearTokenInformation();
			TestFlight.log("Clear Facebook Session in SplashScreen.");
			Log.d("Facebook", "Clear Session.");
		}

		final Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				handleGetInitData();

			}
		}, 1500);
	}
	private void handleGetInitData() {

		LogUtil.v(TAG, "Get Init Data.");

		final AsyncHttpClient client = new AsyncHttpClient();
		client.post(API.GET_INIT_DATA, new AsyncHttpResponseHandler() {

			private final String name = API.GET_INIT_DATA;

			@Override
			public void onStart() {
				LogUtil.i("Call WebService", "WebService : " + name + ", Started...");
			}

			@Override
			public void onFailure(final Throwable e, final String error) {
				LogUtil.e("Call WebService", "WebService : " + name + ", Failure", e);
				LogUtil.w("Call WebService", error);

				if (e instanceof SocketTimeoutException) {
					Toast.makeText(SplashFragment.this, "No Connection, Please try again.", Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(SplashFragment.this, error, Toast.LENGTH_LONG).show();
				}

			}

			@Override
			public void onSuccess(final String res) {

				LogUtil.i("Call WebService", "WebService : " + name + ", Success...");
				LogUtil.i("Call WebService", "Response : " + res);

				try {
					final JSONObject object = new JSONObject(res);

					if (object.getBoolean("success")) {

						final JSONObject msg = object.getJSONObject("message");
						final JSONArray model = msg.getJSONArray("model");
						final JSONArray province = msg.getJSONArray("province");
						final JSONArray brand = msg.getJSONArray("brand");
						final JSONArray dealer = msg.getJSONArray("dealer");
						final JSONArray category = msg.getJSONArray("category");

						SCStaticFunc.setData(SplashFragment.this, SCStaticFunc.MODEL, model);
						SCStaticFunc.setData(SplashFragment.this, SCStaticFunc.PROVINCE, province);
						SCStaticFunc.setData(SplashFragment.this, SCStaticFunc.BAND, brand);
						SCStaticFunc.setData(SplashFragment.this, SCStaticFunc.DEALER, dealer);
						SCStaticFunc.setData(SplashFragment.this, SCStaticFunc.CATEGORY, category);

						changeToLoginActivity();

					} else {
						final String msg = object.getString("message");
						Utils.showToast(SplashFragment.this, msg);
					}
				} catch (final JSONException e) {
					LogUtil.e(SplashFragment.this, TAG, e.getMessage(), e);
				}
			}

			@Override
			public void onFinish() {
				LogUtil.i("Call WebService", "WebService : " + name + ", Finished.");
				Utils.closeAllDialog();
			}
		});
	}

	private void changeToLoginActivity() {
		finish();
		overridePendingTransition(0, 0);
		final Intent intent = new Intent(SplashFragment.this, LoginActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivity(intent);

	}
}